<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: Toolbar for admin, load default data form .../joomzf/toolbar/data.ini
 * 								  load custom data from ...frontend/mod_xyx/controlxyz/toolbar.ini
 *
 * @category   	Joomzf Cms
 * @package    	View
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 May 28, 2012 - 11:21:00 AM
 */
class Joomzf_View_Helper_Toolbar extends Zend_View_Helper_Abstract{
	/**
	 * 
	 * @param String $moduleOptions
	 * @param Zend_Controller_Request_Http $request
	 */
	public function toolbar($fileConfig, $option){
		$view = $this->view;
		$activity = $option->activity;
		$control  = $option->control;
		$option   = $option->option;
		/**	
		 * $mConfig: config from toolbar.ini of each control
		 * $dConfig: default toolbar (data.ini)
		 */
			
		$mConfig = new Joomzf_Config($fileConfig);
		$dConfig = new Joomzf_Config(BASE_PATH.DS.'library'.DS.'joomzf'.DS.'toolbar' .DS.'data.ini');
		/**
		 * $btnList: list of button of each control
		 * $defaultList: list of default button
		 */
		$btnList 	 = explode("|",   $mConfig ->getConfig($activity.".btnList"));
		$defaultList = explode("|",   $dConfig ->getConfig("default.btnList"));
		/**
		 * $btnArray: array of button (full params)
		 */
		$btnArray = array();
		/**
		 * foreach $btnList as name of button
		 */
		if(count($btnList))foreach ($btnList as $btn){
			if($btn == "divider"){
				$btnArray[$btn] = array("name"=>"divider");
			}else{
				$mBtn = $mConfig->getConfig($activity.".".$btn);
				/**
				 * if this button is aready define in data.ini
				 */
				if(array_search($btn, $defaultList) >= 0){
					$dBtn = $dConfig->getConfig($btn);
					/**
					 * infor from $mBtn will overide $dBtn 
					 */
					if(count($mBtn)==0) $mBtn = array();
					
					$mBtn = array_merge($dBtn,$mBtn);
				}
				$btnArray[$btn] = $mBtn;
			}
		}
		/**
		 * display the toolbar
		 */
		foreach ($btnArray as $button){
			$links = $view->jfUrl(array(
					'option'=>$option,
					'control'=>$control,
					'activity'=>$button['target']['activity'])
					,'admin-'.$option.'-'.$control.'-'.$button['target']['activity'],array(),false);
			$xhtml .= $view->toolbarButton(null,$links,null,$button);
		}
		return $xhtml;
	}
}